15. Grails and Hibernate

If GORM (Grails Object Relational Mapping) is not flexible enough for your liking you can alternatively map your domain class using Hibernate. To do this create a hibernate.cfg.xml file in the grails-app/conf/hibernate directory of your project and the corresponding HBM mapping xml files for your domain classes.

For more info on how to do this read the documentation on mapping on the Hibernate Website

This will allow you to map Grails domain classes onto a wider range of legacy systems and be more flexible in the creation of your database schema.

Grails also allows you to write your domain model in Java or re-use an existing domain model that has been mapped using Hibernate. All you have to do is place the necessary hibernate.cfg.xml file and corresponding mappings files in the grails-app/conf/hibernate directory.

Additionally, the good news is you will still be able to call all of the dynamic persistent and query methods allowed in GORM!

15.1 Mapping with Hibernate Annotations

Grails also supports creating domain classes mapped with Hibernate's Java 5.0 Annotations support. To do so you need to tell Grails that you are using an annotation configuration by setting the configClass in your DataSource as follows:

import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration
dataSource {
	configClass = GrailsAnnotationConfiguration.class
   … // remaining properties
}

That's it for the configuration! Make sure you have Java 5.0 installed as this is required to use annotations. Now to create an annotated class we simply create a new Java class in src/java and use the annotations defined as part of the EJB 3.0 spec (for more info on this see the Hibernate Annotations Docs):

package com.books;
@Entity
public class Book {
    private Long id;
    private String title;
    private String description;
    private Date date;

@Id @GeneratedValue public Long getId() { return id; }

public void setId(Long id) { this.id = id; }

public String getTitle() { return title; }

public void setTitle(String title) { this.title = title; }

public String getDescription() { return description; }

public void setDescription(String description) { this.description = description; } }

Once that is done you need to register the class with the Hibernate sessionFactory, to do you need to add entries to the grails-app/conf/hibernate/hibernate.cfg.xml file as follows:

<!DOCTYPE hibernate-configuration SYSTEM
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping package="com.books" />
        <mapping class="com.books.Book" />
    </session-factory>
</hibernate-configuration>

When Grails loads it will register the necessary dynamic methods with the class. To see what else you can do with a Hibernate domain class see the section on Scaffolding.

15.2 Further Reading

Grails committer, Jason Rudolph, took the time to write many useful articles about using Grails with custom Hibernate mappings including: